package com.pipayshop.controller;


import com.pipayshop.domain.vo.PageVo;
import com.pipayshop.domain.vo.ShopInfoVO;
import com.pipayshop.entity.vo.ResponseVO;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.service.OnlineStoreShopService;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiaoqiu
 * @date 2024/9/10 11:46
 * @description 网店店铺接口
 */
@RestController
@RequestMapping("/pipayshopapi/shop")
@Tag(name  = "网店店铺接口",description  = "网店店铺接口")
@Slf4j
public class OnlineStoreShopController {

    @Autowired
    private OnlineStoreShopService shopService;

    @PostMapping("setShopScore")
    @ApiOperation("根据评论评分设置网店评分")
    public ResponseVO setShopScore(){
        try {
            Boolean Residue = shopService.setShopScore();
            if (!Residue) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo(Residue);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("set_review_rating_failed");
        }
    }

    @GetMapping("getUploadBalance/{shopId}")
    @ApiOperation("根据网店id获取商品上架剩余数")
    public ResponseVO<Integer> getUploadBalance(@PathVariable String shopId){
        try {
            Integer uploadBalance = shopService.getUploadBalance(shopId);
            if (uploadBalance == null) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo(uploadBalance);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("get_product_shelf_remaining_failed");
        }
    }

    @GetMapping("getItemInfo/{shopId}/{page}/{limit}/{isDesc}")
    @ApiOperation("根据网店id获取网店商品信息")
    public ResponseVO<PageVo> getItemInfo(@PathVariable("shopId") String shopId,
                                          @RequestParam(required = false, defaultValue = "0") int isDesc,
                                          @RequestParam(required = false, defaultValue = "1") int page,
                                          @RequestParam(required = false, defaultValue = "10") int limit
                                          ) {
        try {
            PageVo pageDataVO = shopService.getShopInfo(shopId,isDesc,page,limit);

            if (pageDataVO == null) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo(pageDataVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("get_store_info_failed");
        }
    }

    @GetMapping("getShopEvaluate/{shopId}/{page}/{limit}")
    @ApiOperation("根据网店id获取网店评价信息")
    public ResponseVO<PageVo> getShopEvaluate(@PathVariable String shopId,@PathVariable Integer page,@PathVariable Integer limit) {
        try {
            PageVo pageDataVO = shopService.getShopEvaluate(shopId,page,limit);
            if (pageDataVO == null) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo(pageDataVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("get_store_info_failed");
        }
    }
    @PostMapping("upVip/{userId}")
    @ApiOperation("根据用户id-网店-升级vip")
    public ResponseVO upVip(@PathVariable("userId") String userId) {
        try {
            boolean b = shopService.upVip(userId);
            if (!b) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo("upgrade_success");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("upgrade_failed");
        }
    }

    @GetMapping("isVip/{userId}")
    @ApiOperation("根据用户id-判断对应网店是否vip")
    public ResponseVO isVip(@PathVariable("userId") String userId) {
        try {
            boolean b = shopService.isVip(userId);
            return ResponseVO.getSuccessResponseVo(b);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("get_member_flag_failed");
        }
    }

    @GetMapping("followList/{userId}")
    @ApiOperation("根据用户id查询 用户关注的网店列表")
    public ResponseVO followList(@PathVariable("userId") String userId, Integer page,Integer limit) {
        try {
            PageVo followList = shopService.getFollowList(userId, page, limit);
            return ResponseVO.getSuccessResponseVo(followList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("query_failed");
        }
    }



    @GetMapping("getItemInfoByUid/{userId}")
    @ApiOperation("根据用户id获取网店信息")
    public ResponseVO getShopInfoByUid(@PathVariable String userId) {
        try {
            List<ShopInfoVO> itemInfo = shopService.getShopInfoByUid(userId);
            return ResponseVO.getSuccessResponseVo(itemInfo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("get_store_info_failed");
        }
    }
    @GetMapping("getShopInfoByShopid/{shopId}")
    @ApiOperation("根据用户id获取网店信息")
    public ResponseVO getShopInfoByShopid(@PathVariable("shopId") String shopId) {
        try {
            List<ShopInfoVO> itemInfo = shopService.getShopInfoByShopid(shopId);
            return ResponseVO.getSuccessResponseVo(itemInfo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("get_store_info_failed");
        }
    }


    @GetMapping("getAllItemInfo/{shopId}/{page}/{limit}/{isDesc}")
    @ApiOperation("根据网店id获取网店商品信息")
    public ResponseVO<PageVo> getAllItemInfo(@PathVariable("shopId") String shopId,
                                          @RequestParam(required = false, defaultValue = "0") int isDesc,
                                          @RequestParam(required = false, defaultValue = "1") int page,
                                          @RequestParam(required = false, defaultValue = "10") int limit
    ) {
        try {
            PageVo pageDataVO = shopService.getAllShopInfo(shopId,isDesc,page,limit);
            if (pageDataVO == null) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo(pageDataVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("get_store_info_failed");
        }
    }
}
